package com.xbongbong.crm.perform.query.render.insert;

import com.xbongbong.crm.abstracts.AbstractCrmNewPageRenderService;
import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.paas.pojo.dto.FormDataAddGetDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.formdata.pojo.SaasSpecialParamPojo;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CommunicatePlanEnum;
import com.xbongbong.saas.enums.business.CustomerCommunicateEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author sunshaoxiang
 * @ClassName CustomerCommunicateNewPageRenderServiceImpl
 * @Description 跟进记录 add get 业务层
 * @Date 2022/6/2 9:31
 * @Version 1.0
 **/
@Service("customerCommunicateNewPageRenderServiceImpl")
public class CustomerCommunicateNewPageRenderServiceImpl extends AbstractCrmNewPageRenderService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerCommunicateNewPageRenderServiceImpl.class);


    @Override
    public List<Integer> businessType() {
        return Collections.singletonList(XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode());
    }

    @Override
    public void businessHandleExplain(FormDataAddGetDTO formDataAddGetDTO, List<FieldAttrEntity> explainList, PaasFormEntityExt form) throws XbbException {
        //关联新建跟进记录入口试会做一些特殊处理
        handelSaasSpecialItem(explainList, formDataAddGetDTO.getBusinessType(), formDataAddGetDTO.getSubBusinessType());

        //重构前 -> com.xbongbong.saas.service.impl.CustomerCommunicateServiceImpl.formatAddExplain
        for (FieldAttrEntity fieldAttrEntity : explainList) {
            String attr = fieldAttrEntity.getAttr();
            if (Objects.equals(attr, CommunicatePlanEnum.NOTIFY_TIME.getAttr())) {
                SaasParticularAttributePoJo saasParticularAttributePoJo = fieldAttrEntity.getSaasParticularAttributePoJo();
                saasParticularAttributePoJo = saasParticularAttributePoJo == null ? new SaasParticularAttributePoJo() : saasParticularAttributePoJo;
                saasParticularAttributePoJo.setDateLimit(DateTimeUtil.getTodayInt());
                saasParticularAttributePoJo.setLimitType(false);
                fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
            } else if (Objects.equals(attr, CustomerCommunicateEnum.COMMUNICATE_TIME.getAttr())) {
                SaasParticularAttributePoJo saasParticularAttributePoJo = fieldAttrEntity.getSaasParticularAttributePoJo();
                saasParticularAttributePoJo = saasParticularAttributePoJo == null ? new SaasParticularAttributePoJo() : saasParticularAttributePoJo;
                saasParticularAttributePoJo.setDateLimit(DateTimeUtil.getTodayInt() + TimeConstant.SECONDS_PER_DAY);
                saasParticularAttributePoJo.setLimitType(true);
                fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
            } else if (Objects.equals(attr, CustomerCommunicateEnum.COMMUNICATE_BUSINESS.getAttr())) {
                SaasSpecialParamPojo saasSpecialParamPojo = formDataAddGetDTO.getSaasSpecialParamPojo();
                if (Objects.nonNull(saasSpecialParamPojo)) {
                    Integer linkBusinessType = saasSpecialParamPojo.getLinkBusinessType();
                    if (Objects.equals(linkBusinessType, XbbRefTypeEnum.CLUE.getCode())) {
                        fieldAttrEntity.setSaasParticularAttributePoJo(new SaasParticularAttributePoJo());
                    }
                }
            }
        }
    }

    /**
     * 关联新建跟进记录入口试会做一些特殊处理
     * 重构前 ->
     * @param explainList
     * @param businessType
     * @param subBusinessType
     */
    private void handelSaasSpecialItem(List<FieldAttrEntity> explainList, Integer businessType, Integer subBusinessType) {
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(subBusinessType);
        switch (xbbRefTypeEnum){
            //客户详情新建跟进记录，非必填以及关联客户
            case DETAIL_CUSTOMER_COMMUNICATE :
            //合同详情新建跟进记录，非必填、关联客户以及跟进业务
            case DETAIL_CONTRACT_COMMUNICATE :
            //机会详情新建跟进记录，非必填以、关联客户以及跟进业务
            case DETAIL_OPPORTUNITY_COMMUNICATE:
            //联系人详情新建跟进记录，非必填以、关联客户以及联系人
            case DETAIL_CONTACT_COMMUNICATE:
            //报价单详情新建跟进记录，非必填、关联客户以及跟进业务
            case DETAIL_QUOTATION_COMUNICATE:
            //线索详情新建跟进记录，非必填、关联客户以及跟进业务
            case DETAIL_CLUE_COMUNICATE:
                explainList.removeIf(fieldAttrEntity -> CustomerCommunicateEnum.detailFilter(fieldAttrEntity.getAttr(), subBusinessType, fieldAttrEntity.getRequired(), fieldAttrEntity.getFieldType()));
                break;
            default:
                break;
        }
    }
}
